/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.hagopa.games.moneypool.dblayer;

import com.mysql.jdbc.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.hagopa.games.moneypool.dblayer.Connector.DBConnector;
import org.hagopa.games.moneypool.game.gameobjects.Property;
import org.hagopa.games.moneypool.game.gameobjects.PropertyStatusEnum;

/**
 *
 * @author Igor
 */
public class GameBoardInfoAdopt implements IDataAdopt{
    private long game_id = 0;
    private int total_fields = 1;
    private ConcurrentHashMap<Integer, Property> fields = new ConcurrentHashMap<Integer, Property>();
    private String dbquery = "";
    
    public GameBoardInfoAdopt(long gameId)
    {
        this.game_id = gameId;
        this.dbquery = "{call mp_get_game_items(?);}";
    }

    public boolean requestDB()
    {
	ResultSet rs = null;
        
        try {

            CallableStatement cs = (CallableStatement) DBConnector.getConn().prepareCall(this.dbquery);
       	    				
       	    cs.setLong(1, this.game_id);
       	    rs = cs.executeQuery();

       	    this.parseDBresponse(rs);
       	    rs.close();
       	    

       	} catch (SQLException e) {
            e.printStackTrace();
            return false;
      	}
        return true;
    }

    public String getQueryType() {
        return "proc";
    }

    public String getQuery() {
        return this.dbquery;
    }

    public boolean parseDBresponse(ResultSet rs) {
        try {
                
                System.out.println("[GameBoardInfoAdopt](parseDBresponse) - Data recieved for Game id:" + game_id);
                while(rs.next())
                {
                    
                    Property tmpprop =  new Property();
                    tmpprop.setName(rs.getString("name"));
                    tmpprop.setPropType(rs.getInt("itm_type"));
                    tmpprop.setCurrCost(rs.getInt("cost"));
                    tmpprop.setGroup(rs.getInt("group_id"));
                    tmpprop.setMortgageCost(rs.getInt("morgage_cost"));
                    tmpprop.setStatus(PropertyStatusEnum.FREE_TO_BUY);
                    tmpprop.setUpgradeLevel(0);
                    tmpprop.setFee(tmpprop.getCurrCost()/10);
                    tmpprop.setOwner(0);
                    System.out.println("[GameBoardInfoAdopt](parseDBresponse) - Assigned property to field  " + this.total_fields);
                    Integer fieldId = new Integer(this.total_fields);
                    fields.put(fieldId,tmpprop);
                    this.total_fields++;

                }
            } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return false;
            }

            return true;
    }
    public Property getFieldById(int fid)
    {
        return this.fields.get(new Integer(fid));
    }
    public List<Property> getFieldsByGroup(int groupId)
    {
        List<Property> cogroup = new ArrayList<Property>();
        Set<Integer> keys = fields.keySet();
        Iterator<Integer> itr = keys.iterator();
        while(itr.hasNext())
        {
            Property prop = fields.get(itr.next());
            if(groupId == prop.getGroup())
                cogroup.add(prop);
        }
        return cogroup;
    }
    public int getTotalFields()
    {
        return this.total_fields;
    }
}
